<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单ajax类
 * @package  ajax/backend/bill
 * @author   allen.qiang
 */

quanli_require('module/base/bill/billCost.class.php');
quanli_require('app_class/goodsHandler.class.php');
quanli_require('module/base/system/sysUser.class.php');

class ajax_billCost {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg']))
        {
            switch($_REQUEST['operFlg']) {

                case 1: //新增或修改报价单
                    $this->searchBillCost();
                    break;

                case 2: //修改状态
                    $this->chanageCostStatus();
                    break;

                case 3: //查询商品
                    $this->searchGoods();
                    break;

                case 4: //保存报价单
                    $this->saveCost();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }
        } else {
            returnJsonData('参数错误');
        }
    }

    /**
     * 查询报价单
     */
    private function searchBillCost ()
    {
        global $G_PGLOB;

        $billCost = new billCost( $G_PGLOB->DBCA );

        $res_arr = $billCost->searchBillCost( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($res_arr) {

            foreach($res_arr as &$cost)
            {
                $statusName = '';
                if($cost->costStatus == 0) $statusName = '创建';
                if($cost->costStatus == 1) $statusName = '已审核';
                if($cost->costStatus == 2) $statusName = '已确认';
                if($cost->costStatus == 9) $statusName = '取消';

                $operHtml = '<a href="javascript:void(0);" onclick="cost.detail(\''.$cost->costCode.'\')">详情</a>&nbsp;&nbsp;';
                if($cost->costStatus == 0){
                    $operHtml .= '<a href="javascript:void(0);" onclick="cost.audit(\''.$cost->costCode.'\')">审核</a>&nbsp;&nbsp;';
                    $operHtml .= '<a href="javascript:void(0);" onclick="cost.cancel(\''.$cost->costCode.'\')">取消</a>&nbsp;';
                }
                $result_array['rows'][] = array(
                    'id' => $cost->costCode,
                    'cell' => array(
                        $operHtml,
                        $statusName,
                        $cost->costCode,
                        $cost->costName,
                        $cost->company_name,
                        $cost->area,
                        $cost->categoryName,
                        $cost->validDate,
                        $cost->createTime
                    )
                );
            }
        }
        echo json_encode($result_array);
    }

    //修改状态
    public function chanageCostStatus(){
        global $G_PGLOB;
        $billCost = new billCost( $G_PGLOB->DBCA );
        $param = array( 'costCode' => $_REQUEST['code'], 'status' => $_REQUEST['status']);
        $res = $billCost->changeCostStatus($param);
        if($res && $res['code'] == 200){
            if($_REQUEST['status'] == 1){
                try{
                    quanli_include('module/base/company/companyMaster.class.php');
                    $company = new companyMaster();
                    $arg = array('company_id' => $res['data']->companyId);
                    $compay_result = $company->search( $arg );
                    if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
                        quanli_include('framework/wx/wxHelp.php');
                        $params = array(
                            'openId' => $compay_result[0]['wx_openid'],
                            'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=1&code='.$_REQUEST['code'],
                            'title' => '报价单提醒',
                            'code' => $_REQUEST['code'],
                            'date' => date('Y-m-d'),
                            'remark' => '您的报价单已经审核通过，点击查看详情'
                        );
                        wxHelp::wxMessage($params);
                        /*推送给对应客户的销售员微信账号*/
                        $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
                        global $G_SHOP;
                        $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
                        $temp_user = $user_master->searchUserInfo($param);
                        if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                            $params = array(
                                'openId' => $temp_user[0]['wx_openid'],
                                'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=1&code='.$_REQUEST['code'],
                                'title' => '报价单提醒',
                                'code' => $_REQUEST['code'],
                                'date' => date('Y-m-d'),
                                'remark' => '大客户'.$compay_result[0]['company_name'].'的报价单已经审核通过，点击查看详情'
                            );
                            wxHelp::wxMessage($params);
                        }
                    }
                }catch (Exception $e){ elog($e->getMessage()); }

                $msg = '审核成功';
            }else{
                $msg = '取消成功';
            }
            returnJsonData($msg, 200);
        }
        returnJsonData('操作失败', 400);
    }

    public function searchGoods(){
        global $G_PGLOB;
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
        $params = array('forsale' => 1);
        if(isset($_REQUEST['categoryId'])){
            $params['bigCategoryID'] = $_REQUEST['categoryId'];
        }
        if(isset($_REQUEST['goodsCode'])){
            $params['postID'] = $_REQUEST['goodsCode'];
        }
        $result_data  = $goodsHandler->searchGoodsSale( $params ) ;

        if(isset($_REQUEST['goodsCode'])) {
            $temp_sync = array();
            if($result_data['syncID']){
                $syncID = explode('|',$result_data['syncID']);
                $syncopate = explode('|',$result_data['syncopate']);
                $temp_sync = array_combine($syncID, $syncopate);
            }
            $temp_birth = array();
            if($result_data['birthID']){
                $birthID = explode('|',$result_data['birthID']);
                $birthcard = explode('|',$result_data['birthcard']);
                $temp_birth = array_combine($birthID, $birthcard);
            }

            $result_data['syncArr'] = $temp_sync;
            $result_data['birthArr'] = $temp_birth;
        }
        returnJsonData('', 200, $result_data);
    }

    //保存报价单
    public function saveCost(){

        if(!isset($_REQUEST['companyId'],$_REQUEST['companyName'],$_REQUEST['costName'],$_REQUEST['area'],$_REQUEST['categoryName'],$_REQUEST['expressId'],$_REQUEST['dtl']) || !is_array($_REQUEST['dtl'])) {
            returnJsonData('缺少参数', 400);
        }

        global $G_PGLOB;

        $billCost = new billCost( $G_PGLOB->DBCA );
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $res_arr = $billCost->saveCost( $_REQUEST );
        if($res_arr['code'] == 200){
            returnJsonData('保存成功', 200);
        }
        returnJsonData('保存失败', 400);
    }
}
